package com.osdp.home.controller;

import com.alibaba.fastjson.JSONObject;
import com.osdp.home.dto.LoginUserVo;
import com.osdp.common.constant.CommonConstant;
import com.osdp.common.exception.PlatformException;
import com.osdp.common.http.Result;
import com.osdp.common.util.ConvertUtil;
import com.osdp.common.util.JWTUtil;
import com.osdp.common.util.MessageUtil;
import com.osdp.common.util.PasswordUtil;
import com.osdp.home.dto.MenuVo;
import com.osdp.home.service.IHomePageService;
import com.osdp.sc.dto.PermissionDTO;
import com.osdp.uc.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Api(tags="用户主界面")
@RestController
@RequestMapping("/home/homePage")
public class HomePageController {

    @Autowired
    private IHomePageService homePageService;
    /**
     * 获取当前用户的信息
     *
     * @param request
     */
    @ApiOperation(value = "获取当前用户的信息")
    @GetMapping("/getUserInfo")
    public Map<String, Object> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.HEADER_ACCESS_TOKEN);
        String userId = JWTUtil.getUserId(token);
        Map<String, Object> map = new HashMap<>(5);
        List<PermissionDTO> permsList = homePageService.queryPermissionsByUserId(userId);
        Set<MenuVo> menuVos = homePageService.getPermissionMenuTree(permsList);
        map.put("menus", menuVos);//当前用户的菜单
        List<PermissionDTO> resources = homePageService.getPermissionButton(permsList);
        map.put("resources",resources);//当前用户的按钮
        LoginUserVo loginUserVo = homePageService.queryUserDetails(userId);
        map.put("userInfo", loginUserVo); //当前用户下的用户相关信息
        return map;
    }

    /**
     * 首页密码修改
     * @param json
     * @return
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
    public Result changPassword(@RequestBody JSONObject json) {
        String username = json.getString("username");
        String oldpassword = json.getString("oldpassword");
        UserDTO user = this.homePageService.queryUserByUserName(username);
        if (StringUtils.isEmpty(username)) {
            return Result.fail(MessageUtil.getMessage(10119));
        }
        String password = json.getString("password");
        String passwordEncode = null;
        try {
            passwordEncode = PasswordUtil.encrypt(username, oldpassword, user.getSalt());
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
        if (!user.getPassWord().equals(passwordEncode)) {
            return Result.fail(MessageUtil.getMessage(10057));
        }
        String confirmpassword = json.getString("confirmpassword");
        if (ConvertUtil.isEmpty(password)) {
            return Result.fail(MessageUtil.getMessage(10058));
        }
        if (!password.equals(confirmpassword)) {
            return Result.fail(MessageUtil.getMessage(10059));
        }
        try {
            PasswordUtil.checkPasswordRule(password, username); // 检测密码强度
            String newpassword = PasswordUtil.encrypt(username, password, user.getSalt());
            homePageService.updatePassWord(user.getId(),newpassword);
        }catch (PlatformException e){
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }
}
